let express = require("express"); // 引入express
let app = express(); // 相当于 http.createServer(app)

//----- 配置跨域 -----
// express框架解决跨域问题的代码，注意该代码要放在 app.use(router); 之前
app.all("*", (req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", "Express");
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

// 访问根路由
app.get("/", (req, res) => {
  // rse.AddHeader("Access-Control-Allow-Origin", "*");
  res.send("Hello World");
});
// 引入user.js
app.use("/user", require("./router/user"));

// 引入news.js
app.use("/news", require("./router/news"));
// 监听3000端口
let server = app.listen(3000, "127.0.0.1", () => {
  let host = server.address().address; // host域
  let port = server.address().port; // 端口号

  console.log(`Server running at http://${host}:${port}`);
});
